using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using MicroFractal.iTunesBrowser.Core.Data.ITunesDB.Header;
using MicroFractal.iTunesBrowser.Core.Helper;

namespace MicroFractal.iTunesBrowser.Core.Data.ITunesDB
{
    public abstract class ITunesDataSet
    {

        #region Attributes
        
        protected MemoryStream _iTunesDataSetChunk;

        #endregion

        #region Constructor
        
        public ITunesDataSet(MemoryStream iTunesDataSetChunk)
        {
            _iTunesDataSetChunk = iTunesDataSetChunk;
            _itemList = new ItemList.ItemList();
        }

        #endregion

        #region Properties

        private DataSetHeader _header;
        /// <summary>
        /// Obtiene o establece el header.
        /// </summary>
        public DataSetHeader Header
        {
            get { return _header; }
            set { _header = value; }
        }

        private ItemList.ItemList _itemList;

        public ItemList.ItemList ItemList
        {
            get { return _itemList; }
            set { _itemList = value; }
        }
	

        #endregion

        internal static List<ITunesDataSet> GetItunesDataSets(MemoryStream stream)
        {
            List<ITunesDataSet> itunesDataSets = new List<ITunesDataSet>();

            int sectionType = 0;
            
            ITunesDataSet _lastDataSet = null;

            while (stream.Length > 0)
            {
                sectionType = ITunesHelper.BytesToInt(stream, 12, 4);
                
                switch ((EnumDataSetType)sectionType)
                { 
                    case EnumDataSetType.AlbumList:
                        _lastDataSet = new ITunesAlbumList(ITunesHelper.GetMemoryStream(stream, (int)stream.Position, ITunesHelper.BytesToInt(stream, 8, 4)));
                        itunesDataSets.Add(_lastDataSet);
                        break;
                    case EnumDataSetType.PlayList:
                        itunesDataSets.Add(new ITunesPlayList(ITunesHelper.GetMemoryStream(stream, (int)stream.Position, ITunesHelper.BytesToInt(stream, 8, 4))));
                        break;
                    //case EnumDataSetType.PodCastList:
                    //    itunesDataSets.Add(ITunesPodCast.GetPodCastList(GetDataSet(stream)));
                    //    break;
                    //case EnumDataSetType.SmartPlayList:
                    //    itunesDataSets.Add(ITunesSmartList.GetSmartList(GetDataSet(stream)));
                    //    break;
                    case EnumDataSetType.TrackList:
                        _lastDataSet = new ITunesTrackList(ITunesHelper.GetMemoryStream(stream, (int)stream.Position, ITunesHelper.BytesToInt(stream, 8, 4)));
                        itunesDataSets.Add(_lastDataSet);
                        break;
                    default:
                        _lastDataSet = null;
                        break;
                }

                if (_lastDataSet != null)
                    stream = ITunesHelper.GetMemoryStream(stream, _lastDataSet.Header.TotalLength);
                else
                {
                    stream = ITunesHelper.GetMemoryStream(stream, ITunesHelper.BytesToInt(stream, 8, 4));
                }
            }

            return itunesDataSets;
        }
       
    }
}
